<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">

        <el-form-item>
          <span>状态：</span>
          <el-radio-group v-model="selectOptions.status" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in statusMaps" :label="item.value" name="status" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <span>命令：</span>
          <el-radio-group v-model="selectOptions.type" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in typeMaps" :label="item.value" name="type" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table ref="multipleTable" :data="taskPoolList" border tooltip-effect="dark" v-loading="listLoading" style="width: 100%" @selection-change="selectionChange" element-loading-text="玩命请求中。。。">
      <!--<el-table-column type="selection" width="55">
      </el-table-column>-->
      <el-table-column prop="id" width="125" label="任务id">
      </el-table-column>
      <el-table-column prop="from_source_key" label="来源期数" width="100">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" :formatter="showStatus">
      </el-table-column>
      <el-table-column prop="area_name" label="区域名" width="120">
      </el-table-column>
      <el-table-column prop="filed_name" label="地块" width="120">
      </el-table-column>
      <el-table-column prop="name" label="运营姓名">
      </el-table-column>
      <el-table-column prop="worker_name" label="管家姓名">
      </el-table-column>
      <el-table-column prop="type" label="命令" :formatter="showType">
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间":formatter="createTime">
      </el-table-column>
      <el-table-column prop="update_time" label="修改时间" width="200">
      </el-table-column>

      <!--<el-table-column label="操作" width="150">-->
        <!--<template scope="scope">
          <div style="padding: 5px" v-if="scope.row.status == 2">
            <el-button type="success">不可更改</el-button>
          </div>
          <div style="padding: 5px" v-else-if="scope.row.accept_time === '0'">
            <el-button @click="" type="info" @click="reWorkpatch(scope.row)">更改</el-button>
          </div>
          <div style="padding: 5px" v-else>
            <el-button @click="" type="warning">不可更改</el-button>
          </div>-->
          <!--<el-button type="danger" size="small" @click="singleDeleteField(scope.row.id)">删除</el-button>-->
        <!--</template>-->
      <!--</el-table-column>-->

    </el-table>

    <!--分页-->
    <el-col :span="24" class="toolbar">
      <!--<el-button type="danger" @click="batchDistribute" :disabled="this.multipleSelection.length===0">批量分配任务</el-button>-->
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

    <!--分配任务弹框-->
    <el-dialog title="分配任务" v-model="dispatchForm" :close-on-click-modal="false" style="margin-top: 100px">

      <el-form :model="editForm" label-width="80px">
        <el-radio-group v-model="work_uid">
          <el-badge :value="item.taskNum" class="item" style="margin-left: 20px;margin-top: 10px;" v-for="(item, index) in workAcceptedTask" :key="index">
              <el-radio-button :label="item.id">{{ item.name }}</el-radio-button>
          </el-badge>
        </el-radio-group>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dispatchForm = false">取消</el-button>
        <el-button type="primary" @click.native="dispatchWorker" :loading="listLoading"  v-if="!islist">分配</el-button>

        <el-button type="primary" @click.native="reDispatchWorker" :loading="listLoading" v-if="islist">更改</el-button>
      </div>

    </el-dialog>

  </section>
</template>

<script type="text/ecmascript-6">
 // import { getTaskPoolList, getworkerUndoTask, dispatchWorkerTask, reDispatchWorkerTask } from '../../api'
  import util from '../../util'
  import NProgress from 'nprogress'
	import { apiTree } from '../../apiTree'
	
  export default {
    data() {
      return {
        filters: {
          name: ''
        },
        work_uid: 0,
        statusMaps: [
          {value: 0, label: '未分配'},
          {value: 1, label: '已分配'},
          {value: 2, label: '已完成'}
        ],
        typeMaps: [
          {value: -1, label: '全部'},
          {value: 3, label: '锄草'},
          {value: 4, label: '施肥'},
          {value: 5, label: '除虫'},
          {value: 6, label: '浇水'},
          {value: 7, label: '确认收菜时间'}
        ],
        reId: '',
        listLoading: false,
        total: 0,
        page: 1,
        type: '',
        islist: false,
        taskPoolList: [],
        multipleSelection: [],
        editForm: {},
        dispatchForm: false,
        workAcceptedTask: [],
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          type: this.$route.query.type !== undefined ? this.$route.query.type : -1,
          status: this.$route.query.status !== undefined ? this.$route.query.status : 0
        }
      }
    },
    methods: {
      currentStatusChange() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      createTime(row) {
        return row.create_time === '0' ? 0 : this.formatTime(row.create_time)
      },
      showStatus(row) {
        if (row.status === '0') {
          return '未分配'
        } else if (row.status === '1') {
          return '已分配'
        } else {
          return '已完成'
        }
      },
      formatTime(_time) {
        return util.formatDate.format(new Date(_time * 1000), 'yyyy-MM-dd hh:mm:ss').substring(2)
      },
      showType(row) {
        if (row.type === '3') {
          return '锄草'
        } else if (row.type === '4') {
          return '施肥'
        } else if (row.type === '5') {
          return '除虫'
        } else if (row.type === '6') {
          return '浇水'
        } else if (row.type === '7') {
          return '确认收菜时间'
        }
      },
      reWorkpatch(row) {
        this.reId = row.id
        this.work_uid = 0
        this.type = row.type
        this.islist = true
        apiTree('task/index', 'task/workerUndoTask').then((res) => {
          this.workAcceptedTask = res.d
          this.dispatchForm = true
        })
      },
      // 重新分配任务
      reDispatchWorker() {
        let para = {'id': this.reId, 'work_uid': this.work_uid, 'om_uid': this.$store.state.adminer.om_uid, 'type': this.type}
        apiTree('task/index', 'task/reDispatchWorkerTask',para).then((res) => {
          if (res.s === -3) {
            this.$message({
              message: '该任务已被接单不可分配',
              type: 'success'
            })
          }
          this.dispatchForm = false
          this.getTaskPoolList()
        })
      },
      // 批量分配任务
      batchDistribute() {
        this.work_uid = 0
        this.islist = false
        apiTree('task/index','task/workerUndoTask').then((res) => {
          this.workAcceptedTask = res.d
          this.dispatchForm = true
        })
      },
      dispatchWorker() {
        let ids = this.multipleSelection.map(item => item.id).toString()
        let para = {'taskIds': ids, 'work_uid': this.work_uid, 'om_uid': this.$store.state.adminer.om_uid}
        if (this.work_uid === 0) {
          this.$message('未选择工作人员')
          return false
        }
        apiTree('task/index','task/dispatchWorkerTask',para).then((res) => {
          if (res.s === 1 || res.s === -3) {
            this.$message({
              message: '任务分配成功',
              type: 'success'
            })
            this.work_uid = 0
            this.dispatchForm = false
            this.getTaskPoolList()
          } else {
            this.$message.error('该运营人员无权分配')
          }
        })
      },
      getTaskPoolList() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            status: 0,
            type: -1
          }
        }
        this.listLoading = true;
        NProgress.start();
        apiTree('task/index','task/index',para).then((res) => {
          this.taskPoolList = res.d.data
          this.total = res.d.total_num
          this.listLoading = false
          NProgress.done();
        })
      },
      selectionChange(val) {
        this.multipleSelection = val;
      },
      _reload() {
        this.getTaskPoolList();
      },
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      }
    },
    created () {
      this.getTaskPoolList()
    },
    watch: {
      $route: 'getTaskPoolList'
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">

</style>
